<?php
namespace \DragonU\DB;
/**
 * CorePress DB Process library.
 *
 * @internal
 * Copyright (c) 2009, Jacob Santos
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *  * Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  * Neither the name of the DragonU nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * @author Jacob Santos <wordpress@santosj.name>
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD license
 * @package DragonU
 * @subpackage DB
 * @since 0.1
 */

/**
 * CorePress DB Process class.
 *
 * Stores all of the table names that are used with the prefix. This will take
 * the table and add the prefix to it. Therefore, those using the class can use
 * the default tables with the set prefix without neededing to know the given
 * prefix or add their own using the class.
 *
 * @author Jacob Santos
 * @since 0.1
 * @package DragonU
 * @subpackage DB
 */
class Process
{

	/**
	 * The default database prefix.
	 *
	 * @since 0.1
	 * @var string
	 */
	protected $prefix = '';

	/**
	 * Set the default database prefix.
	 *
	 * @since 0.1
	 * @param string $prefix Optional. Default prefix.
	 */
	public function __construct($prefix = false)
	{
		if ( is_string( $prefix ) )
			$this->prefix = $prefix;
	}

	/**
	 * Retrieve or set the default prefix.
	 *
	 * @since 0.1
	 *
	 * @param string $prefix Optional. Default prefix.
	 * @return string Default prefix.
	 */
	public function prefix($prefix = false)
	{
		if ( false !== $prefix && is_string( $prefix ) )
		{
			$this->prefix = $prefix;
		}

		return $this->prefix;
	}

	/**
	 * Retrieve the table with prefix.
	 *
	 * @todo Add sanitization to table name.
	 * @since 0.1
	 *
	 * @param string $name Table name.
	 * @return string Table name with prefix.
	 */
	public function table($name)
	{
		if ( is_string($name) )
			return $this->prefix() . $name;

		return '';
	}

	/**
	 * Retrieve the table with custom prefix.
	 *
	 * If no prefix is given, then the default prefix will be used instead.
	 */
	public function tableWithPrefix($name, $prefix = false)
	{
		if( false !== $prefix && is_string( $prefix ) && is_string( $name ) )
		{
			return $prefix . $name;
		}

		if( false === $prefix && is_string( $name ) )
		{
			return $this->prefix() . $name;
		}

		return '';
	}

}